package com.xzr.spring;

import com.xzr.test.Person;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class MultitonFactory2 {
    static Map<String, String> map = new HashMap<>();

    static {
        //1.读取配置文件
        Properties properties = new Properties();
        InputStream resourceAsStream = MultitonFactory2.class.getResourceAsStream("beans.properties");
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (String key : properties.stringPropertyNames()) {
            String property = properties.getProperty(key);
            map.put(key, property);
        }
    }

    public Person getBean(String key) {
        Person p = null;
        try {
            p = (Person) Class.forName(map.get(key)).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return p;
    }

}
